package com.abacus.pos.base

import android.content.Context
import android.os.Bundle

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import androidx.annotation.StringRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment

abstract class BaseFragment<T : ViewDataBinding> : Fragment() {

    /*指定xml文件*/
    abstract fun getLayoutResource(): Int

    /*在这个方法中实现viewmodel的初始化*/
    abstract fun initVM()

    /*在这个方法里实现数据初始化动作*/
    abstract fun startObserve()


    private lateinit var dataBinding: T
    private lateinit var baseActivity: BaseActivity<*>
    val binding: T
        get() {
            if (dataBinding == null)
                throw RuntimeException("请检查是否在OnDestroyView之后使用binding对象")
            return dataBinding!!
        }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        dataBinding = DataBindingUtil.inflate(inflater, getLayoutResource(), container, false)
        dataBinding.lifecycleOwner = viewLifecycleOwner
        return dataBinding?.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        baseActivity = activity as BaseActivity<*>
        initVM()
        startObserve()
    }

    fun showToast(@StringRes resId: Int, duration: Int = Toast.LENGTH_SHORT) {
        baseActivity.showToast(resId, duration)
    }

    fun showToast(str: String, duration: Int = Toast.LENGTH_SHORT) {
        baseActivity.showToast(str, duration)
    }

    open fun showDialog() {
        baseActivity.showDialog()
    }

    open fun hideDialog() {
        baseActivity.hideDialog()
    }


    /**
     * 隐藏软键盘
     */
    fun hideSoftInput() {
        val imm = baseActivity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(activity?.window?.decorView?.windowToken, 0)
    }


    /**
     * fragment 内部切换fragment
     */
    fun switchChildFragment(containerViewId: Int, fragment: Fragment) {
        baseActivity.handler.post {
            var transaction = childFragmentManager.beginTransaction()
            transaction.replace(containerViewId, fragment, fragment::class.java.name)
            transaction.commitAllowingStateLoss()
            childFragmentManager.executePendingTransactions()
        }
    }

    /**
     * fragment调用baseactivity切换framgent
     */
    fun switchFragment(
        fragment: Fragment,
        addToBackStack: Boolean = true,
        clearStack: Boolean = false
    ) {
        baseActivity.switchFragment(
            fragment,
            addToBackStack,
            clearStack
        )
    }

    fun performBackClick() {
        baseActivity.handler.post { activity?.supportFragmentManager?.popBackStackImmediate() }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        hideDialog()
    }

}
